package com.ruoyi.patient.entity;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.service.domain.ServiceServed;

import java.util.Calendar;

public class PatientServedVO {
    private Long servedId;

    /** 姓名 */
    @Excel(name = "姓名")
    private String servedName;

    /** 身份证号 */
    @Excel(name = "身份证号")
    private String servedIdnumber;

    /** 年龄 */
    @Excel(name = "年龄")
    private Integer servedAge;

    /** 手机号码 */
    @Excel(name = "手机号码")
    private String servedTelephonenumber;

    /** 用户性别（0男 1女 2未知） */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private String servedSex;

    private String servedAddress;

    ///就诊号
    private String servedEpid;

    private String relation;
            ;
    private String isDefault;


    public Long getServedId() {
        return servedId;
    }

    public void setServedId(Long servedId) {
        this.servedId = servedId;
    }

    public String getServedName() {
        return servedName;
    }

    public void setServedName(String servedName) {
        this.servedName = servedName;
    }

    public String getServedIdnumber() {
        return servedIdnumber;
    }

    public void setServedIdnumber(String servedIdnumber) {
        this.servedIdnumber = servedIdnumber;
    }

    public Integer getServedAge() {
        return servedAge;
    }

    public void setServedAge(Integer servedAge) {
        this.servedAge = servedAge;
    }

    public String getServedTelephonenumber() {
        return servedTelephonenumber;
    }

    public void setServedTelephonenumber(String servedTelephonenumber) {
        this.servedTelephonenumber = servedTelephonenumber;
    }

    public String getServedSex() {
        return servedSex;
    }

    public void setServedSex(String servedSex) {
        this.servedSex = servedSex;
    }

    public String getServedAddress() {
        return servedAddress;
    }

    public void setServedAddress(String servedAddress) {
        this.servedAddress = servedAddress;
    }

    public String getServedEpid() {
        return servedEpid;
    }

    public void setServedEpid(String servedEpid) {
        this.servedEpid = servedEpid;
    }

    public String getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }

    public String getRelation() {
        return relation;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }

    public PatientServedVO() {
    }

    public PatientServedVO(ServiceServed newServed) {
        this.servedId = newServed.getServedId();
        this.servedName = newServed.getServedName();
        this.servedIdnumber = newServed.getServedIdnumber();
        this.servedAge = getPersonAgeFromIdCard(newServed.getServedIdnumber());
        this.servedTelephonenumber = newServed.getServedTelephonenumber();
        this.servedEpid = newServed.getServedVisitid();
        this.servedSex = getSex(newServed.getServedSex());
        this.relation = newServed.getServedConnect();
        this.isDefault = newServed.getServedDefaultFlag();
    }

    /**
     * 根据身份证号计算年龄
     * @param servedIdnumber
     * @return
     */
    public Integer getPersonAgeFromIdCard(String servedIdnumber) {

        //截取身份证中出行人出生日期中的年、月、日
        Integer personYear = Integer.parseInt(servedIdnumber.substring(6, 10));
        Integer personMonth = Integer.parseInt(servedIdnumber.substring(10, 12));
        Integer personDay = Integer.parseInt(servedIdnumber.substring(12, 14));

        Calendar cal = Calendar.getInstance();
        // 得到当前时间的年、月、日
        Integer yearNow = cal.get(Calendar.YEAR);
        Integer monthNow = cal.get(Calendar.MONTH) + 1;
        Integer dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        Integer yearMinus = yearNow - personYear;
        Integer monthMinus = monthNow - personMonth;
        Integer dayMinus = dayNow - personDay;

        Integer age = yearMinus; //先大致赋值

        if (yearMinus == 0) { //出生年份为当前年份
            age = 0;
        }else{ //出生年份大于当前年份
            if (monthMinus < 0) {//出生月份小于当前月份时，还没满周岁
                age = age - 1;
            }
            if (monthMinus == 0) {//当前月份为出生月份时，判断日期
                if (dayMinus < 0) {//出生日期小于当前月份时，没满周岁
                    age = age - 1;
                }
            }
        }
        return age;
    }

    public String getSex(String sex) {

        if ("0".equals(sex)){
            return "男";
        }else if("1".equals(sex)){
            return "女";
        }else{
            return "未知";
        }

    }


}
